View Javadoc
1   package edu.jiangxin.apktoolbox.file.password.recovery.checker;
2   
3   import org.apache.poi.poifs.crypt.Decryptor;
4   import org.apache.poi.poifs.crypt.EncryptionInfo;
5   import org.apache.poi.poifs.filesystem.POIFSFileSystem;
6   
7   import java.io.FileInputStream;
8   import java.io.FileNotFoundException;
9   import java.io.IOException;
10  import java.security.GeneralSecurityException;
11  
12  public class XmlBasedOfficeChecker extends FileChecker {
13      private static final boolean DEBUG = false;
14  
15      public XmlBasedOfficeChecker() {
16          super();
17      }
18  
19      @Override
20      public String[] getFileExtensions() {
21          return new String[]{"docx", "pptx", "xlsx"};
22      }
23  
24      @Override
25      public String getFileDescription() {
26          return "*.docx;*.pptx;*.xlsx";
27      }
28  
29      @Override
30      public String getDescription() {
31          return "Office File Checker(XML-based formats)";
32      }
33  
34      @Override
35      public boolean prepareChecker() {
36          return true;
37      }
38  
39      @Override
40      public boolean checkPassword(String password) {
41          if (DEBUG) {
42              logger.info("checkPassword: {}", password);
43          }
44          boolean result = false;
45          try (POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(file))) {
46              EncryptionInfo info = new EncryptionInfo(pfs);
47              Decryptor decryptor = Decryptor.getInstance(info);
48              result = decryptor.verifyPassword(password);
49          } catch (FileNotFoundException e) {
50              logger.error("checkPassword FileNotFoundException");
51          } catch (IOException e) {
52              logger.error("checkPassword IOException");
53          } catch (GeneralSecurityException e) {
54              logger.error("checkPassword GeneralSecurityException");
55          }
56          return result;
57      }
58  }